import 'package:flutter_menu/data/obx_store.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:objectbox/objectbox.dart';

import '../objectbox.g.dart';

part 'shope_model.g.dart';

///店铺信息
@Entity()
@JsonSerializable()
class ShopModel with IEntity{
  @Id()
  int obxId;
  ///类别(商户等级)：1百弓，2分公司，3总部，4分部，5门店
  int? category;

  ///国家（0：印尼，1：墨西哥）
  int? country;

  ///店铺uuid
  @Index()
  String? id;

  ///店铺名称
  String? name;

  ///店铺logo
  String? shopLogo;

  ///原料关联可售数量
  bool? enableMaterialLinkProduct;


  ///市别信息
  @JsonKey(fromJson: MarketTypeModel.toManyFromJson, toJson: MarketTypeModel.toManyJson)
  ToMany<MarketTypeModel> marketTypeList = ToMany<MarketTypeModel>();

  ///店铺id
  int shopNumber;

  ShopModel({
    this.obxId = 0,
    this.updateTime = 0,
    this.category,
    this.country,
    this.id,
    this.name,
    this.shopLogo,
    this.shopNumber = 0,
  });

  factory ShopModel.fromJson(Map<String, dynamic> json) =>
      _$ShopModelFromJson(json);

  Map<String, dynamic> toJson() => _$ShopModelToJson(this);

  static ToOne<ShopModel> toOneFromJson(Map<String, dynamic> json) =>
      ToOne(target: ShopModel.fromJson(json));

  static Map<String, dynamic>? toOneJson(ToOne<ShopModel> s) =>
      s.target != null ? _$ShopModelToJson(s.target!) : {};

  static ToMany<ShopModel> toManyFromJson(List<dynamic>? json) =>
      ToMany(items: json?.map((e) => ShopModel.fromJson(e)).toList());

  static List<Map<String, dynamic>>? toManyJson(ToMany<ShopModel>? s) =>
      s?.toList().map((e) => e.toJson()).toList();



  @override
  int updateTime;

  @override
  int update(Store store, int? time) {
    updateTime = time?? updateTime;
    var box =  store.box<ShopModel>();
    marketTypeList.forEach((e)=> e.obxId = e.update(store,time));
    return box.put(this);
  }



}

@Entity()
@JsonSerializable()
class ShopTableInfoModel with IEntity{
  @Id()
  int obxId;

  ///店铺桌台区域名称
  String? areaName;

  ///桌台区域uuid

  @Index()
  String? areaUuid;

  ///序号
  int sort;

  ///最大可坐人数
  int tableMaxNumber;

  @JsonKey(fromJson: TableModel.toManyFromJson, toJson: TableModel.toManyJson)
  ToMany<TableModel> tables = ToMany<TableModel>();

  ///房台数量
  int? tablesNumber;

  ShopTableInfoModel(
      {this.areaName,
      this.areaUuid,
      this.updateTime = 0,
      this.sort = 0,
      this.tableMaxNumber = 0,
      this.tablesNumber,
      this.obxId = 0});

  factory ShopTableInfoModel.fromJson(Map<String, dynamic> json) =>
      _$ShopTableInfoModelFromJson(json);

  Map<String, dynamic> toJson() => _$ShopTableInfoModelToJson(this);

  static ToOne<ShopTableInfoModel> toOneFromJson(Map<String, dynamic> json) =>
      ToOne(target: ShopTableInfoModel.fromJson(json));

  static Map<String, dynamic>? toOneJson(ToOne<ShopTableInfoModel> s) =>
      s.target != null ? _$ShopTableInfoModelToJson(s.target!) : {};

  static ToMany<ShopTableInfoModel> toManyFromJson(List<dynamic>? json) =>
      ToMany(items: json?.map((e) => ShopTableInfoModel.fromJson(e)).toList());

  static List<Map<String, dynamic>>? toManyJson(
          ToMany<ShopTableInfoModel>? s) =>
      s?.toList().map((e) => e.toJson()).toList();

  @override
  int updateTime;

  @override
  int update(Store store, int? time) {
    updateTime = time?? updateTime;
    tables.forEach((e)=> e.obxId = e.update(store,time));
    var box =  store.box<ShopTableInfoModel>();
    return box.update(this, ShopTableInfoModel_.areaUuid, areaUuid);
  }
}

@Entity()
@JsonSerializable()
class TableModel  with IEntity{
  @Id()
  int obxId;

  ///店铺桌台最多就餐人数
  int maxNum;

  ///店铺桌台最少就餐人数
  int minNum;

  ///序号
  int sort;

  ///店铺桌台名称
  String? tableName;

  ///桌台uuid
  @Index()
  String? tableUuid;

  TableModel(
      {this.maxNum = 0,
      this.minNum = 0,
      this.sort = 0,
      this.updateTime = 0,
      this.tableName,
      this.tableUuid,
      this.obxId = 0});

  factory TableModel.fromJson(Map<String, dynamic> json) =>
      _$TableModelFromJson(json);

  Map<String, dynamic> toJson() => _$TableModelToJson(this);

  static ToOne<TableModel> toOneFromJson(Map<String, dynamic> json) =>
      ToOne(target: TableModel.fromJson(json));

  static Map<String, dynamic>? toOneJson(ToOne<TableModel> s) =>
      s.target != null ? _$TableModelToJson(s.target!) : {};

  static ToMany<TableModel> toManyFromJson(List<dynamic>? json) =>
      ToMany(items: json?.map((e) => TableModel.fromJson(e)).toList());

  static List<Map<String, dynamic>>? toManyJson(ToMany<TableModel>? s) =>
      s?.toList().map((e) => e.toJson()).toList();

  @override
  int updateTime;

  @override
  int update(Store store, int? time) {
    updateTime = time?? updateTime;
    var box =  store.box<TableModel>();
    return box.update(this, TableModel_.tableUuid, tableUuid);

  }
}

@Entity()
@JsonSerializable()
class MarketTypeModel with IEntity{
  @Id()
  int obxId;

  ///主键id
  @Index()
  String? id;

  ///市别code
  String? code;

  /// 开始时间
  String? beginTime;

  /// 结束时间
  String? endTime;

  @override
  int updateTime;

  MarketTypeModel({this.obxId = 0, this.id, this.code, this.beginTime, this.updateTime = 0});

  factory MarketTypeModel.fromJson(Map<String,dynamic> json) => _$MarketTypeModelFromJson(json);
  Map<String, dynamic> toJson() => _$MarketTypeModelToJson(this);

  static ToOne<MarketTypeModel> toOneFromJson(Map<String, dynamic> json) =>
  ToOne(target: MarketTypeModel.fromJson(json));

  static Map<String, dynamic>? toOneJson(ToOne<MarketTypeModel> s) =>
  s.target != null ? _$MarketTypeModelToJson(s.target!) : {};

  static ToMany<MarketTypeModel> toManyFromJson(List<dynamic>? json) =>
  ToMany(items: json?.map((e) => MarketTypeModel.fromJson(e)).toList());

  static List<Map<String, dynamic>> toManyJson(ToMany<MarketTypeModel> s) =>
  s.toList().map((e) => e.toJson()).toList();


  @override
  int update(Store store, int? time) {
    updateTime = time?? updateTime;
    var box =  store.box<MarketTypeModel>();
    return box.update(this, MarketTypeModel_.id, id);
  }
}

@Entity()
@JsonSerializable()
class DeptModel with IEntity{
  @Id()
  int obxId;

  ///主键id
  @Index()
  String? id;


  ///部门类型 1 普通 2 厨房 3 仓库
  int? type;

  ///是否默认 1 默认主仓库 不能删除
  int isDefault;

  /// 排序
  int? sort;

  @override
  int updateTime;

  DeptModel({this.obxId =0, this.id, this.type, this.isDefault = 0, this.sort, this.updateTime = 0});

  @override
  int update(Store store, int? time) {
    updateTime = time?? updateTime;
    var box =  store.box<DeptModel>();
    return box.update(this, DeptModel_.id, id);
  }

  factory DeptModel.fromJson(Map<String,dynamic> json) => _$DeptModelFromJson(json);
  Map<String, dynamic> toJson() => _$DeptModelToJson(this);

  static ToOne<DeptModel> toOneFromJson(Map<String, dynamic> json) =>
  ToOne(target: DeptModel.fromJson(json));

  static Map<String, dynamic>? toOneJson(ToOne<DeptModel> s) =>
  s.target != null ? _$DeptModelToJson(s.target!) : {};

  static ToMany<DeptModel> toManyFromJson(List<dynamic>? json) =>
  ToMany(items: json?.map((e) => DeptModel.fromJson(e)).toList());

  static List<Map<String, dynamic>> toManyJson(ToMany<DeptModel> s) =>
  s.toList().map((e) => e.toJson()).toList();
}